<template>
  <div>
    <el-card>
      <div slot="header">
        <my-breadcrumb>评论管理</my-breadcrumb>
      </div>

      <!-- 表格 -->
      <el-table :data="comments">
        <el-table-column label="文章标题" prop="title"> </el-table-column>
        <el-table-column label="总评论数" prop="total_comment_count">
        </el-table-column>
        <el-table-column label="粉丝评论数" prop="fans_comment_count">
        </el-table-column>
        <el-table-column label="评论状态">
          <template slot-scope="scope">
            <!-- {{scope.row.comment_status}} -->
            <el-tag v-if="scope.row.comment_status">正常</el-tag>
            <el-tag v-else type="warning">关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              v-if="scope.row.comment_status"
              @click="hSwitch(scope.row)"
              >关闭评论</el-button
            >
            <el-button
              size="mini"
              v-else
              type="danger"
              @click="hSwitch(scope.row)"
              >打开评论</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 列表分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="per_page"
        @current-change="hPageChange"
        :total="total_count"
        :disabled="isLoading"
      >
      </el-pagination>
      <!-- /列表分页 -->
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'comment',
  data () {
    return {
      total_count: 0, // 总条数
      page: 1, // 页码
      per_page: 20,
      isLoading: false,
      comments: []
    };
  },
  methods: {
    buildParams () {
      const params = {
        page: this.page,
        per_page: this.per_page,
        response_type: 'comment'
      };
      return params;
    },
    async getCommentList () {
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/articles',
          method: 'GET',
          params: this.buildParams()
        });
        console.log(res);
        this.comments = res.data.data.results;
      } catch (err) {
        console.log(err);
      }
    },
    // 点击分页器回调
    hPageChange (curpage) {
      this.page = curpage;
      // 重新请求数据
      this.getCommentList();
    },
    async doSwitch (row) {
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/comments/status',
          method: 'PUT',
          params: {
            article_id: row.id.toString()
          },
          data: {
            // 当前的状态取反
            allow_comment: !row.comment_status
          }
        })
        console.log(res);
        // 更新本地的数据
        row.comment_status = !row.comment_status
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err);
      }
    },
    hSwitch (row) {
      console.log(row);
      // 当前评论状态是true,即为允许，要更改为false,即要关闭评论
      if (row.comment_status) {
        this.$confirm('您确定要关闭评论吗').then(() => {
          this.doSwitch(row)
        }).catch(() => {
          this.$message.error('操作失败')
        })
      } else {
        this.doSwitch(row)
      }
    }
  },
  created () {
    this.getCommentList();
  }
};
</script>

<style>
</style>
